package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.TOrderBean;
import cst.neu.yuejian_31.iface.TOrderIface;
import cst.neu.yuejian_31.iface.TOrderListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.TOrderService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/30.
 */

public class TOrderModel implements TOrderIface {
    private Retrofit retrofit;
    public TOrderModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getTOreder(String userId, final TOrderListener tOrderListener) {
        TOrderService tOrderService=retrofit.create(TOrderService.class);
        Call<List<TOrderBean>>call=
                tOrderService.getTOreder(userId);
        call.enqueue(new Callback<List<TOrderBean>>() {
            @Override
            public void onResponse(Call<List<TOrderBean>> call, Response<List<TOrderBean>> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    tOrderListener.onSuccess(response.body());
                }else {
                    tOrderListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<List<TOrderBean>> call, Throwable t) {
                tOrderListener.onFail(t.toString());

            }
        });

    }
}
